package com.muyu;

import org.elasticsearch.index.query.*;

/**
 * @ClassName: QueryBuilders
 * @Author: Zirun.Xin
 * @Date: 2025/4/28 上午8:52
 * @Version: 1.0
 * @Description: 组合查询案例
 */
public class QueryBuildersDemo {

    /**
     * BoolQuery使用
     * must 相当于 与 & =
     * mustNot 相当于 非 ~ ! =
     * should 相当于 或 | or
     * filter 过滤
     */
    private void boolQuery() {
        QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("content", "test1"))
                .must(QueryBuilders.termQuery("content", "test4"))
                .mustNot(QueryBuilders.termQuery("content", "test2"))
                .should(QueryBuilders.termQuery("content", "test3"))
                .filter(QueryBuilders.termQuery("content", "test5"));
    }

    /**
     * 精确查询
     * 以下字段名用${fieldName}代替，具体值用${fieldValue}代替
     */
    private void preciseQuery() {
        /* 数字查询 */
        // 单个
        TermQueryBuilder onlyNumberTermQueryBuilder =
                QueryBuilders.termQuery("${fieldName}", "${fieldValue}");

        // 批量
        TermsQueryBuilder manyNumberTermQueryBuilder =
                QueryBuilders.termsQuery("${fieldName}", "${fieldValues}");

        /* 字符串查询 */
        // 单个
        TermQueryBuilder onlyStringTermQueryBuilder =
                QueryBuilders.termQuery("${fieldName}.keyword", "${fieldValue}");

        // 批量
        TermsQueryBuilder manyStringTermQueryBuilder =
                QueryBuilders.termsQuery("${fieldName}.keyword", "${fieldValues}");
    }

    /**
     * 模糊查询
     * 以下字段名用${fieldName}代替，具体值用${fieldValue}代替
     */
    private void likeQuery() {
        /* 数字查询都为精确查询 */

        /* 字符串查询 */
        MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = QueryBuilders.moreLikeThisQuery(
                new String[]{"${fieldName}"}, new String[]{"${fieldValue"}, null
        );
    }

    /**
     * 区间查询
     * 以下字段名用${fieldName}代替，具体值用${fieldValue}代替
     */
    private void rangeQuery() {
        /* 数字查询 */
        // 闭区间
        RangeQueryBuilder rqb1 = QueryBuilders.rangeQuery("${fieldName}")
                .from("${fieldValue1}").to("${fieldValue2}");

        // 开区间
        RangeQueryBuilder rqb2 = QueryBuilders.rangeQuery("${fieldName}")
                .from("${fieldValue1}", false)
                .to("${fieldValue2}", false);

        // 大于
        RangeQueryBuilder rqb3 = QueryBuilders.rangeQuery("${fieldName}").gt("${fieldValue}");

        // 大于等于
        RangeQueryBuilder rqb4 = QueryBuilders.rangeQuery("${fieldName}").gte("${fieldValue}");

        // 小于
        RangeQueryBuilder rqb5 = QueryBuilders.rangeQuery("${fieldName}").lt("${fieldValue}");

        // 小于等于
        RangeQueryBuilder rqb6 = QueryBuilders.rangeQuery("${fieldName}").lte("${fieldValue}");

        /* 多条件查询 */
        MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = QueryBuilders.moreLikeThisQuery(
                new String[]{"${fieldName1}"}, new String[]{"${filedValue1}"}, null
        );
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders
                .rangeQuery("${fieldName2}").gt("${fieldValue2}");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(moreLikeThisQueryBuilder).must(rangeQueryBuilder);
    }
}
